<?php
/**
 * Created by PhpStorm.
 * User: fanfz
 * Date: 2015/12/8 0008
 * Time: 23:20
 */
namespace Admin\Controller;
use Think\Controller;

class ScoreController  extends AuthController {
    public function index()
    {
        $Score = M('Score');
        //搜索条件
        $search=I('search',array() );

        $std_id=$search[ 'std_id' ];
        $lesson_id=$search[ 'lesson_id' ];

        if( $std_id&&isset( $std_id ) ){
            $filter[ 'std_id' ]=$std_id;
        }

        if( $lesson_id&&isset( $lesson_id ) ){
            $filter[ 'lesson_id' ]=$lesson_id;
        }
        //分页
        $total = $Score->where( $filter )->count();
        if( $total ){
            $perNum=25;
            $Page=new \Think\Page( $total , $perNum );

            $Page->setConfig( 'prev' , "上一页" );//上一页
            $Page->setConfig( 'next' , '下一页' );//下一页
            $Page->setConfig( 'first' , '首页' );//第一页
            $Page->setConfig( 'last' , "末页" );//最后一页
            $Page->setConfig( 'theme' , '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%' );

            $show=$Page->show();

            $this->assign( 'total' , $total );
            $this->assign( 'page' , $show );

        }

        $score_list = $Score->where( $filter )->order('std_id asc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('score_list', $score_list);

        $this->display('score_list');
    }

    //添加科
    public function add()
    {
        $this->display('score_add');
    }

    //编辑科
    public function edit()
    {
        $id = I('id');
        $score_info = M('Score')->where(array('Id' => $id))->find();
        $this->assign('score_info', $score_info);
        $this->display('score_add');
    }

    //保存数据
    public function save()
    {
        $id = I('id');
        $lesson_id=I('lesson_id', '');
        $std_id=I('std_id', '');

        $data['std_id']  = $std_id;
        $data['lesson_id']  = $lesson_id;
        $Score= M('Score');
        $info = $Score->where($data)->find();
        $data['score']  = I('score', '');
        if($info){
            //更新操作
            $result = $Score->where(array('Id' => $id))->save($data);
            if($result || $result === 0){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '成绩修改成功,正在转跳到专业列表'));
            }else if($result === FALSE){
                $this->ajaxReturn(array('status' => 'error', 'info' => '成绩修改失败'));
            }
        }else{
            //入库操作
            $result = $Score->add($data);
            if($result){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '成绩添加成功,正在转跳到专业列表'));
            }else if($result){
                $this->ajaxReturn(array('status' => 'error', 'info' => '成绩添加失败'));
            }
        }

    }

    public function del()
    {
        $id=I( 'id' );
        $Score= M('Score');
        $result=$Score->delete( $id );
        if($result){
            $data[ 'status' ]=1;
            $data[ 'data' ]='成绩删除成功';
        }else if($result){
            $data[ 'status' ]=0;
            $data[ 'data' ]='成绩删除失败,请刷新重试';
        }
        $this->ajaxReturn( $data );
    }
}